package com.ellricky.baselib.ui

import android.util.Log
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.ui.base.BaseTakePhotoActivity
import com.ellricky.baselib.util.*
import kotlinx.android.synthetic.main.activity_fill_visit_record.*
import org.devio.takephoto.model.TResult
import java.lang.StringBuilder
import java.util.*

/**
 * 回访记录
 */
class FillVisitRecordActivity : BaseTakePhotoActivity() {

    private val memberId: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_ID) ?: ""
    }

    private var img1: String = ""
    private var img2: String = ""
    private var img3: String = ""

    private var index = 0

    override fun getContentLayout(): Int {
        return R.layout.activity_fill_visit_record
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = "填写回访记录"

        ivPic.setOnClickListener {
            index = 0
            EDialogUtil.showTakePhotoDialog(this, this)
        }

        ivPic2.setOnClickListener {
            index = 1
            EDialogUtil.showTakePhotoDialog(this, this)
        }

        ivPic3.setOnClickListener {
            index = 2
            EDialogUtil.showTakePhotoDialog(this, this)
        }

        btnSubmit.setOnClickListener {
            val imageBuilder =
                StringBuilder().append(img1).append(";").append(img2).append(";").append(img3)
            val remark = et_remark.text.toString()
            val url = EAppUtil.getApiHost()
            val paramMap: MutableMap<String, String> = HashMap()
            paramMap["action"] = getActions()
            paramMap["return_remark"] = remark
            paramMap["member_id"] = memberId
            paramMap["eid"] = LocalCacheUtil.getUid()
            paramMap["img"] = imageBuilder.toString()

            HttpUtils.getInstance().postFrom(url, paramMap, object : NewEntityCallback<Any?>(this) {

                override fun onSuccess(var1: Any?, var2: String?) {
                    ToastUtil.show(this@FillVisitRecordActivity, "添加成功")
                    finish()
                }
            })
        }
    }

    private fun getActions(): String {
        when (LocalCacheUtil.getCardType()) {
            BaseConstant.CARD_TEACHER_MANAGER -> {
                return "gzaAddReturnRemark"
            }
        }
        return "gzaAddReturnRemark"
    }

    override fun takeSuccess(result: TResult?) {
        val picPath = result?.image?.compressPath ?: ""
        // 接口提交
        if (EmptyUtil.isEmpty(picPath)) {
            ToastUtil.show(this, "图片路径为空")
            return
        }
        if (index == 0) {
            img1 = FileUtil.fileToBase64(result?.image?.originalPath ?: "")
            if (!EmptyUtil.isEmpty(picPath)) {
                GlideUtil.into(this, picPath, ivPic)
            }
        }
        if (index == 1) {
            img2 = FileUtil.fileToBase64(result?.image?.originalPath ?: "")
            if (!EmptyUtil.isEmpty(picPath)) {
                GlideUtil.into(this, picPath, ivPic2)
            }
        }
        if (index == 2) {
            img3 = FileUtil.fileToBase64(result?.image?.originalPath ?: "")
            if (!EmptyUtil.isEmpty(picPath)) {
                GlideUtil.into(this, picPath, ivPic3)
            }
        }
        Log.e("pic", picPath)

    }
}